//
//  HGNewMusicViewController.m
//  悦享
//
//  Created by dllo on 16/3/31.
//  Copyright © 2016年 张洪国. All rights reserved.
//

#import "HGNewMusicViewController.h"
#import "SongModel.h"
#import "HGMusicTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "HGMvViewController.h"
@interface HGNewMusicViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *dataArr;/**<  装数据原的数组 */
@property (nonatomic, retain) UITableView *tableView;/**<   */
@property (nonatomic, retain) UIImageView *image1;/**<  头部图片 */
@property (nonatomic, retain) UINavigationBar *navBar;

@property (nonatomic, retain) UILabel *titleLabel;/**<  标题 */
@end

@implementation HGNewMusicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor redColor];
    [self handleData];
    
    [self createTableView];
    
    self.navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, SREEN_WIDTH, 64)];
    
    
    _navBar.alpha = 0;
    [self.view addSubview:_navBar];
    
    self.image1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SREEN_WIDTH, 200)];
    [_tableView addSubview:_image1];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
    _titleLabel.center = _navBar.center;
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.textColor = [UIColor whiteColor];
    _titleLabel.font = [UIFont boldSystemFontOfSize:21];
    [_navBar addSubview:_titleLabel];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(20, 20, 30, 30);
    [backButton setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    
}

//协议条状
- (void)jumpToMVVc:(NSNotification *)notification{
    
    HGMvViewController *hgmv = [[HGMvViewController alloc] init];
    hgmv.model = notification.object;
    
    [self presentViewController:hgmv animated:YES completion:^{
        
    }];
    
}
- (void)buttonAction:(UIButton *)button{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)createTableView{
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SREEN_WIDTH, SREEN_HEIGHT)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    _tableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
    
    [self.view addSubview:_tableView];
    
    
    [_tableView registerClass:[HGMusicTableViewCell class] forCellReuseIdentifier:@"HGMusicTableViewCell"];
    
    self.image1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, -200, SREEN_WIDTH, 200)];
    [_tableView addSubview:_image1];
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat yOffset = scrollView.contentOffset.y;
    CGFloat xOffset = (yOffset + 200) * 2;
    if (yOffset < -200) {
        CGRect rect = _image1.frame;
        rect.origin.y = yOffset;
        rect.size.height = -yOffset;
        rect.origin.x = xOffset;
        rect.size.width = self.view.frame.size.width + fabs(xOffset) * 2;
        _image1.frame = rect;
        
    }
    
    CGFloat alpha = (yOffset + 200) / 200;
    
    [UIView animateWithDuration:0.5 animations:^{
        
        _navBar.alpha = alpha;
        NSLog(@"%f", alpha);
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return _dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HGMusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HGMusicTableViewCell"];
   
    
    SongModel *model1 = _dataArr[indexPath.row];
    
    
    if (model1.mvListArr.count == 0) {
        
        model1.haveMV = NO;
    }else{
        model1.haveMV = YES;
    }
    
    cell.model = model1;
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    
    
    NSString *url = [NSString stringWithFormat:@"http://api.songlist.ttpod.com/songlists/%@?app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPhone5S&f=f320&s=s310&imsi=&hid=&splus=9.2.1&active=1&net=2&openudid=72d26f93c0799c325993cf509c2472027f13fa1d&idfa=EA226D01-40F9-413A-A388-906DF3F490F7&utdid=Vu6ZOeT0fJwDAFqPovwyz9bO&alf=201200&bundle_id=com.ttpod.music&latitude=38.8826056689678&longtitude=121.5393642649618%ld", _value, indexPath.row];
    
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"NewMusicChangeValue" object:url];
    
    
    
}


- (void)handleData{
    
    NSString *rul = [NSString stringWithFormat:@"http://api.dongting.com/song/album/%@?app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPhone5S&f=f320&s=s310&imsi=&hid=&splus=9.2.1&active=1&net=2&openudid=72d26f93c0799c325993cf509c2472027f13fa1d&idfa=EA226D01-40F9-413A-A388-906DF3F490F7&utdid=Vu6ZOeT0fJwDAFqPovwyz9bO&alf=201200&bundle_id=com.ttpod.music&latitude=38.88249303864156&longtitude=121.5394091135701", _value];
    
    [APPTools GETWithURL:rul par:nil success:^(id responseObject) {
        
        NSDictionary *data = responseObject[@"data"];
        
        NSArray *songList = data[@"songList"];
        
        self.dataArr = [SongModel baseModelHandleWithArr:songList];
        
        NSLog(@"%@", _dataArr);
        
        _titleLabel.text = data[@"name"];
      
        
        SongModel *model = [_dataArr firstObject];
        
        [_image1 sd_setImageWithURL:[NSURL URLWithString:model.picUrl] placeholderImage:[UIImage imageNamed:@"place"]];
        
        
        [_tableView reloadData];
        
    } filed:^(NSError *error) {
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
